package com.boss.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boss.entity.Job;
import com.boss.mapper.JobMapper;
import com.boss.service.JobService;
import com.boss.vo.JobVo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class JobServiceImpl extends ServiceImpl<JobMapper,Job> implements JobService {
    @Autowired
    private JobMapper jobMapper;
    public ArrayList<JobVo> getJobAll(){
        ArrayList<JobVo> jobVos = new ArrayList<>();
        jobMapper.selectList(null).forEach(job ->{
            JobVo jobVo = new JobVo();
            BeanUtils.copyProperties(job,jobVo);
//            处理有下划线的字段名
            jobVo.setCompany_cover(job.getCompanyCover());
            jobVo.setCompany_intro(job.getCompanyIntro());
            jobVo.setCompany_size(job.getCompanySize());
//            处理数组字段
//            context
            ArrayList<String> context = new ArrayList<String>();
            if(job.getContext0()!=null){context.add(job.getContext0());}
            if(job.getContext1()!=null){context.add(job.getContext1());}
            if(job.getContext2()!=null){context.add(job.getContext2());}
            if(job.getContext3()!=null){context.add(job.getContext3());}
            if(job.getContext4()!=null){context.add(job.getContext4());}
            if(job.getContext5()!=null){context.add(job.getContext5());}
            jobVo.setContext(context);

//            label
            ArrayList<String> label = new ArrayList<String>();
            if(job.getLabel0()!=null){label.add(job.getLabel0());}
            if(job.getLabel1()!=null){label.add(job.getLabel1());}
            if(job.getLabel2()!=null){label.add(job.getLabel2());}
            jobVo.setLabel(label);

            jobVos.add(jobVo);
        });
//        jobVos.forEach(System.out::println);
        return jobVos;
    }
    public JobVo getJobById(String id){
        JobVo jobVoActive = new JobVo();
        jobMapper.selectList(null).forEach(job ->{
            JobVo jobVo = new JobVo();
            BeanUtils.copyProperties(job,jobVo);
//            处理有下划线的字段名
            jobVo.setCompany_cover(job.getCompanyCover());
            jobVo.setCompany_intro(job.getCompanyIntro());
            jobVo.setCompany_size(job.getCompanySize());
//            处理数组字段
//            context
            ArrayList<String> context = new ArrayList<String>();
            if(job.getContext0()!=null){context.add(job.getContext0());}
            if(job.getContext1()!=null){context.add(job.getContext1());}
            if(job.getContext2()!=null){context.add(job.getContext2());}
            if(job.getContext3()!=null){context.add(job.getContext3());}
            if(job.getContext4()!=null){context.add(job.getContext4());}
            if(job.getContext5()!=null){context.add(job.getContext5());}
            jobVo.setContext(context);

//            label
            ArrayList<String> label = new ArrayList<String>();
            if(job.getLabel0()!=null){label.add(job.getLabel0());}
            if(job.getLabel1()!=null){label.add(job.getLabel1());}
            if(job.getLabel2()!=null){label.add(job.getLabel2());}
            jobVo.setLabel(label);

            if(jobVo.getId().equals(id)){
                BeanUtils.copyProperties(jobVo,jobVoActive);
            }
        });
        return jobVoActive;
    }

    @Override
    public ArrayList<JobVo> toJobVoList(List<Job> records) {
        ArrayList<JobVo> jobVos = new ArrayList<>();
        for (Job job : records) {
            JobVo jobVo = new JobVo();
            BeanUtils.copyProperties(job, jobVo);
//            处理有下划线的字段名
            jobVo.setCompany_cover(job.getCompanyCover());
            jobVo.setCompany_intro(job.getCompanyIntro());
            jobVo.setCompany_size(job.getCompanySize());
//            处理数组字段
//            context
            ArrayList<String> context = new ArrayList<>();
            if (job.getContext0() != null) {
                context.add(job.getContext0());
            }
            if (job.getContext1() != null) {
                context.add(job.getContext1());
            }
            if (job.getContext2() != null) {
                context.add(job.getContext2());
            }
            if (job.getContext3() != null) {
                context.add(job.getContext3());
            }
            if (job.getContext4() != null) {
                context.add(job.getContext4());
            }
            if (job.getContext5() != null) {
                context.add(job.getContext5());
            }
            jobVo.setContext(context);

//            label
            ArrayList<String> label = new ArrayList<>();
            if (job.getLabel0() != null) {
                label.add(job.getLabel0());
            }
            if (job.getLabel1() != null) {
                label.add(job.getLabel1());
            }
            if (job.getLabel2() != null) {
                label.add(job.getLabel2());
            }
            jobVo.setLabel(label);

            jobVos.add(jobVo);
        }
        return jobVos;
    }

}
